package cn.edu.jgsu.jgsuchat.service;

import cn.edu.jgsu.jgsuchat.pojo.customPojo.SelectFriendsByUserId;
import cn.edu.jgsu.jgsuchat.pojo.databasePojo.Friends;
import cn.edu.jgsu.jgsuchat.pojo.databasePojo.User;
import cn.edu.jgsu.jgsuchat.mapper.UserDao;
//import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 作者: xyn
 * 创建时间: 2020/3/25 上午4:28
 */
@Service
public class RedisDataService {
    @Autowired

    private RedisTemplate redisTemplate = null;
/*    @Autowired
    private UserDao userDao;*/

    @Autowired
    private StringRedisTemplate stringRedisTemplate = null;

    /**
     * 为
     *
     * @param updateWhoseChatStatus
     * @param status
     */
    public void updateMessageStatus(String updateWhoseChatStatus, String updateWhoSendChat, String status) {

        redisTemplate.opsForHash().put(updateWhoseChatStatus, updateWhoSendChat, status);
    }

    public int storeChatData(String sendToWho, String whoSend, String sendWhat) {
        int list = whoSend.hashCode();
        System.out.println(whoSend + "的消息的list表是" + list);
        stringRedisTemplate.opsForHash().put(sendToWho + "mM", whoSend, String.valueOf(list));
        //创建一张消息表list
        stringRedisTemplate.opsForList().leftPush(String.valueOf(list), sendWhat);
        updateMessageStatus(sendToWho, whoSend, "1");
        return 1;
    }

    public void testSample() {
        storeChatData("熊一能", "吴泽奇", "天气冷,吃火锅");
        storeChatData("熊一能", "吴泽奇", "去不去");
        storeChatData("熊一能", "吴泽奇", "再叫两个人");
        storeChatData("熊一能", "唐凯", "中午去哪吃");
        storeChatData("熊一能", "江庆旺", "美食美客");
        storeChatData("熊一能", "江庆旺", "黄焖鸡");
        storeChatData("江庆旺", "熊一能", "行啊去黄焖鸡");
        storeChatData("江庆旺", "熊一能", "美食美客也行,我都可以");
    }

 /*   public List<SelectFriendsByUserId> getFriendsById(int id) {
        List<SelectFriendsByUserId> list = userDao.selectFriendsByUserId(id);
        return list;
    }*/

    public Map getMessage(String getWhoseChatData, String getWhoSend) {
        String hashcode = String.valueOf(getWhoSend.hashCode());
        BoundListOperations listOps = stringRedisTemplate.boundListOps(hashcode);
        long i = listOps.size();
        System.out.println(getWhoSend + "发送的消息有多少条?: " + i);
        HashMap map = new HashMap<>();
        LinkedList list = new LinkedList();
        for (long j = 0; j < i; j++) {
            list.add(listOps.rightPop());
        }
        map.put("message", list);
        map.put("getWhoSend", getWhoSend);
        updateMessageStatus(getWhoseChatData, getWhoSend, "0");
        return map;
    }
/*

    public int loadAllUsersStatus() {
        //相当于给每个user创建一个Hash,起名可以叫做Status+UserId
        //由于对于redis来说,给hash存值不用实现创建hash表,所以我们可以用几个for循环即可
        //外层循环遍历user列表
        //内存循环遍历存放一个user的每个friend

        List<User> userList = userDao.selectAllUsersId();
        for (User user : userList) {
            List<Friends> friends = userDao.selectFriendsIdByUserId(user.getId());
            for (Friends friend : friends) {
                updateMessageStatus(String.valueOf(user.getId()), String.valueOf(friend.getFriendId()), "0");
            }
        }
        return 1;
    }
*/

    public List hasNewMessage(String getWhoseChatData) {
        //获取1709104021的状态列表
        Map map = redisTemplate.opsForHash().entries(getWhoseChatData);
        //changeList返回遍历过程中发现值改变了的集合,等待后续操作

        List changeList = new LinkedList<>();
        for (Object key : map.keySet()) {
            if (!"0".equals(map.get(key))) {
                changeList.add(key);
            }
        }
        if (changeList.size() == 0) {
            System.out.println("无新消息");
            return null;

        } else {
            for (Object o : changeList) {
                System.out.println(getWhoseChatData + "的" + o + "消息列表有改动");

            }
            return changeList;

        }

    }
}

